package xml.uidElements;

import java.util.ArrayList;

import xml.others.UIDException;
import xml.uidElements.entry.AbstractDataItem;
import xml.uidElements.entry.AbstractStructure;
import xml.uidElements.entry.EnumeratedData;
import xml.uidElements.entry.ORDataEntry;
import xml.uidElements.entry.SubState;
import xml.uidElements.entry.XORDataEntry;
import xml.uidElements.output.AbstractOutputDataItem;
import xml.uidElements.output.AbstractOutputStructure;

public class State extends AbstractState {

	public State(String idState) {
		super(idState);
		// TODO Auto-generated constructor stub
	}

	public void insertSubState(SubState subState) {

		if (listElements == null) {
			this.listElements = new ArrayList<UIDNewElement>();
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);
			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(subState.getId()))
				throw new UIDException(UIDException.EXISTING_SUBSTATE,
						"There is already a substate with this name");
		}
		listElements.add(subState);

	}

	public void insertOutputDataItemInSubState(String idSubState,
			AbstractOutputDataItem dataItem) {

		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);
			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSubState)) {
				((AbstractState) elem).insertOutputDataItem(dataItem);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
	}

	public void insertInputDataItemInSubState(String idSubState,
			AbstractDataItem dataItem) {
		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);
			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSubState)) {
				((AbstractState) elem).insertInputDataItem(dataItem);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");

	}

	public void insertInputEnumeratedDataInSubState(String idSubState,
			EnumeratedData enumeratedData) {

		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);
			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSubState)) {
				((AbstractState) elem)
						.insertInputEnumeratedData(enumeratedData);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");

	}

	public void insertInputXORDataEntryInSubState(String idSubState,
			XORDataEntry xorDataEntry) {

		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);
			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSubState)) {
				((AbstractState) elem).insertInputXORDataEntry(xorDataEntry);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");

	}

	public void insertInputORDataEntryInSubState(String idSubState,
			ORDataEntry orDataEntry) {

		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);
			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSubState)) {
				((AbstractState) elem).insertInputORDataEntry(orDataEntry);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");

	}

	public void insertInputStructureInSubState(String idSubState,
			AbstractStructure structure) {

		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);
			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSubState)) {
				((AbstractState) elem).insertInputStructure(structure);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");

	}

	public void insertOutputStructureInSubState(String idSubState,
			AbstractOutputStructure structure) {

		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);

			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSubState)) {
				((AbstractState) elem).insertOutputStructure(structure);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");

	}

	public void insertTextInSubState(String idSubState, Text text) {

		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);

			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSubState)) {
				((AbstractState) elem).insertText(text);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");

	}

	public void insertTransitionFromSubState(String idSourceSubState,
			Transition transition) {

		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);

			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSourceSubState)) {
				((AbstractState) elem).insertOutgoingTransition(transition);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");

	}

	public void insertTransitionFromDataItemInSubState(String idSourceSubState,
			String idSourceDataItem, Transition transition) {

		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);

			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSourceSubState)) {
				((AbstractState) elem).insertTransitionFromDataItem(
						idSourceDataItem, transition);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");

	}

	public void insertTransitionFromStructureInSubState(
			String idSourceSubState, String idSourceStructure,
			Transition transition) {

		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);

			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSourceSubState)) {
				((AbstractState) elem).insertTransitionFromStructure(
						idSourceStructure, transition);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");

	}

	public void insertTransitionFromSetDataItemInSubState(
			String idSourceSubState, String idSourceDataItem,
			TransitionFromSet transition) {


		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);

			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSourceSubState)) {
				((AbstractState) elem).insertTransitionFromSetDataItem(
						idSourceDataItem, transition);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");

		
	}

	public void insertTransitionFromSetStructureInSubState(
			String idSourceSubState, String idSourceStructure,
			TransitionFromSet transition) {


		if (listElements == null) {
			throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");
		}

		for (int i = 0; i < this.listElements.size(); i++) {
			UIDNewElement elem = this.listElements.get(i);

			if (elem instanceof AbstractState
					&& ((AbstractState) elem).getId().equals(idSourceSubState)) {
				((AbstractState) elem).insertTransitionFromSetStructure(
						idSourceStructure, transition);
				return;
			}
		}
		throw new UIDException(UIDException.NOT_EXISTING_SUBSTATE, "");

		
	}

}
